package com.ruisi.weatherreport.protocol;

import com.loopj.android.http.AsyncHttpClient;
import com.ruisi.weatherreport.cities.CityManager;
import com.ruisi.weatherreport.protocol.WeatherInterfaces.DetailWeatherListener;

/**
 * 业务协议支持
 * 所有的天气相关查询均通过WeatherProtocol提供的接口来查询
 * 
 * @author spring ming
 *
 */

public class WeatherProtocol {
	
	/**
	 * 协议错误码
	 */
	public static final int PE_JSON_PARSE_ERR  = 1;
	public static final int PE_CITY_NOT_EXSIST = 2;
	
	// 协议客户端，所有的请求通过sWeatherClient来发起
	private static final AsyncHttpClient sWeatherClient = new AsyncHttpClient();
	
	private static final String DETAIL_WEATHER_IFNO_BASE_URL = "http://m.weather.com.cn/data/";
	
	/**
	 * 根据城市名字查询详细天气信息
	 * @param cityName
	 * @param listener
	 * @return
	 */
	public static int queryDetailWeatherInfo(String cityName, DetailWeatherListener listener){
		String cityId = CityManager.singleInstance().getCityCode(cityName);
		if(null == cityId){
			// 如果没有查到cityId，直接回调
			listener.onFail(PE_CITY_NOT_EXSIST, "city not exsist.");
			return 0;
		}
		
		String url = DETAIL_WEATHER_IFNO_BASE_URL + cityId + ".html";
		sWeatherClient.get(url, new DetailWeatherInfoHandler(listener));
		
		return 0;
	}

}
